package com.mall.ums.controller;

import com.mall.model.domain.ums.ResourceCategory;
import com.mall.ums.service.ResourceCategoryService;
import com.mall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author clutchcitylee
 * @create 2023-03-13-18:54
 */
@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 添加后台资源分类
     *
     * @param resourceCategory
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody ResourceCategory resourceCategory) {
        resourceCategory.setCreateTime(new Date());
        resourceCategoryService.save(resourceCategory);
        return R.success("添加分类成功");
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<String> deleteById(@PathVariable Integer id) {
        resourceCategoryService.removeById(id);
        return R.success("删除分类成功");
    }

    /**
     * 查询所有后台资源分类
     *
     * @return
     */
    @GetMapping("/listAll")
    public R<List<ResourceCategory>> listAll() {
        List<ResourceCategory> resourceCategories = resourceCategoryService.list();
        return R.success(resourceCategories);
    }

    /**
     * 修改后台资源分类
     *
     * @param id
     * @param resourceCategory
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> updateById(@PathVariable Integer id, @RequestBody ResourceCategory resourceCategory) {
        resourceCategoryService.updateById(resourceCategory);
        return R.success("修改分类成功");
    }
}
